# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.6 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=2.79
_minorchar=b
[[ -z ${_minorchar} ]] && {
  pkgver=${_basever}
} || {
  pkgver=${_basever}.${_minorchar}
}
pkgrel=4
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
license=('GPL')
# buid conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
#optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !debug)
install=blender-${CARCH}.install
source=(https://download.blender.org/source/${_realname}-${_basever}${_minorchar}.tar.gz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0003-mingw-dont-install-3rdparty.patch
        0004-fix-finding-glew.patch
        0005-fix-define-syntax.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0007-mingw-DEBUG-dont-suffix-libs-with-_d.patch
        0008-mingw-use-X11-like-path-layout.patch
        0010-link-blenlib-after-intern_gpudirect.patch
        0011-c++11-defines-__i386__-not-i386.patch
        0012-re-enable-SSE-on-mingw-w64-i686.patch
        0013-provide-sprintf-for-comutil.patch
        0014-libstdc++-not-support-utf-stream.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0017-find-pugixml.patch
        0018-msvc-stuff.patch
        0100-ffmpeg-4-compatibility.patch)
sha256sums=('4c944c304a49e68ac687ea06f5758204def049b66dc211e1cffa1857716393bc'
            '5f654367edbdadeae5cb54acd27b5d62788ce4463f6aca8ed973b7c7b43a6069'
            '305977db08dc9ac1ca13ce1b1c49589b7190123f9017593199f51e8e9f44d8b3'
            '9ac81ace5d40a860ea6aa72d3212cc1b9096c350835d0ef8c81b8010fbb02a4c'
            '0cd0fb76502e8325db0400ab1845f91fd0525ff76a22294c3e8031314fb4430f'
            '87036a005a817bce9edd5fb7f18ba3b7a0b8d309d6f1f9bb0f813c931d889c0d'
            '8449c0769ff16b00189715f12275b25442e2b6b7ef4e47797d3229c79b1bb35b'
            '083d6658053ac29200efefe05ea55f4815a7e254f4a1f98347507c7c29704ee6'
            'ea9fa273debbce153999a64d32b6df2207d90d02133f23adbf967e7f28417fcb'
            'cfd0678e1a33577726232243d44785dae12e352a88e0075bd78c85c17fb75cfd'
            '411461cdf710d3a42564e661d15adbbccb1e669aec2aa09b10f9c6836baee872'
            'c47eb70aabec8945a35760c7781292756545c704fa745ef57c735719493a34cf'
            '58e7a84fd67e12457ced9e8d494215fe76ad8372489942695689a8043c054f06'
            '4be69e29a45ccd07ad1a8e59071431b1870412686b1934117c303370e0d4dc38'
            '0e5536cc4e9c571846cce47b6ee5c57807d8c17b20c4b0565c2efb6fab9b8c76'
            '375535461c8dacd215ca79f7a350f6d4ca8ef48fa97c517363ec3fae640c0472'
            '5ce05eed73408045c78cd1be262393b2c6815b77dd2979964a8090e00a3ce5ac'
            'aae1bafccfd9492197139f2c26dfb4e61fa665acb1bdd569d06ce043b5e9810b'
            '52c9c20fef6e4570d36c266737412121dd7d52d2f5032635d524e042aa2a943f')

prepare() {
  cd ${srcdir}/${_realname}-${_basever}${_minorchar}
  [[ -d build_files/cmake/platform/platform_win32_mingw64.cmake ]] && rm build_files/cmake/platform/platform_win32_mingw64.cmake
  patch -p1 -i "${srcdir}"/0001-mingw-buildsystem.patch
  patch -p1 -i "${srcdir}"/0002-fix-mingw-w64-definitions.patch
  patch -p1 -i "${srcdir}"/0003-mingw-dont-install-3rdparty.patch
  patch -p1 -i "${srcdir}"/0004-fix-finding-glew.patch
  patch -p1 -i "${srcdir}"/0005-fix-define-syntax.patch
  patch -p1 -i "${srcdir}"/0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
  patch -p1 -i "${srcdir}"/0007-mingw-DEBUG-dont-suffix-libs-with-_d.patch
  patch -p1 -i "${srcdir}"/0008-mingw-use-X11-like-path-layout.patch
  patch -p1 -i "${srcdir}"/0010-link-blenlib-after-intern_gpudirect.patch
  patch -p1 -i "${srcdir}"/0011-c++11-defines-__i386__-not-i386.patch
  patch -p1 -i "${srcdir}"/0012-re-enable-SSE-on-mingw-w64-i686.patch
  patch -p1 -i "${srcdir}"/0013-provide-sprintf-for-comutil.patch
  patch -p1 -i "${srcdir}"/0014-libstdc++-not-support-utf-stream.patch
  patch -p1 -i "${srcdir}"/0015-mingw-python-like-msvc.patch
  patch -p1 -i "${srcdir}"/0016-mingw-windres.patch
  patch -p1 -i "${srcdir}"/0017-find-pugixml.patch
  patch -p1 -i "${srcdir}"/0018-msvc-stuff.patch
  patch -p0 -i "${srcdir}"/0100-ffmpeg-4-compatibility.patch

}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir "${srcdir}"/build-${CARCH}  && cd "${srcdir}"/build-${CARCH}

  # i686 SSE is problematic with Cycles: https://developer.blender.org/T36316
  # .. but commit:
  # https://lists.blender.org/pipermail/bf-blender-cvs/2013-October/059705.html
  # .. breaks the build:
  # undefined reference to `ccl::kernel_cpu_sse2_path_trace` .. etc
  # This is because it prevents the definition of __KERNEL_SSE2__ without having
  # ensured WITH_CYCLES_OPTIMIZED_KERNEL_SSE2 is undefined. Let's hope that the
  # bugs have been fixed as patch 0011 re-enables __KERNEL_SSE2__. If they're not
  # fixed then we'll have to pass ENABLESSE2 to cmake instead, -DWITH_CPU_SSE being
  # the correct one (probably!)
  # [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF -DWITH_CPU_SSE=OFF"
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_OPENJPEG=ON \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_ALEMBIC=OFF \
    -DWITH_GAMEENGINE=ON \
    -DWITH_RAYOPTIMIZATION=ON \
    -DWITH_JACK=OFF \
    -DWITH_PLAYER=ON \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=python${_pyver}m \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${_pyver}m \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DPYTHON_NUMPY_PATH=${MINGW_PREFIX}/lib/python${_pyver}/site-packages \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_IMAGE_REDCODE=ON \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}-${_basever}${_minorchar}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR=${pkgdir} install
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe
  rm ${pkgdir}${MINGW_PREFIX}/blender.exe

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}"
  ${MINGW_PREFIX}/bin/python3 -m compileall \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/addons
}
